<?php
/**
 * hr管理可根据条件申请列表
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Attendance_UserInfo');

class Bll_Attendance_Examine extends BLL{

    /**
     * 搜索列表折分查询，选了申请时间的，如果有选用户就指定用户,默认不限用户
     * @param arrayg $where
     * @param string $order
     * @param int $limit
     * @param int $offset
     * @return multitype:
     */
    public function get_examine_list($where,$limit=20,$offset=0,$order=" apply_time_ext DESC "){
        $data = array(
            'count'=>0,
            'datalist'=>array()
        );
        if(empty($where)){
            return $data;
        }
        $_user_where=array();
        $_where = array();
        $user_data=array();

        if($where['citys'] || $where['co_name'] || $where['apart']){//如果选的城市较多，可能会很慢
            if(!empty($where['citys'])){
                $city_name="'".implode("','",$where['citys'])."'";
                $_user_where[" city_name IN($city_name) AND 1=?"]=1;
            }
            if(!empty($where['co_name'])){
                $_user_where["code LIKE '%{$where['co_name']}%' OR name LIKE '%{$where['co_name']}%' AND 1=?"] = 1;
            }
            if(!empty($where['apart'])){
                $_user_where["department_name='{$where['apart']}' AND 1=?"] = 1;
            }
            $user_data = $this->get_user_info($_user_where);
            $uids = array_keys($user_data);
            $_where[" user_id IN(".implode(',',$uids).") AND 1=?"]=1;
        }

        if(!empty($where['apply_start']) && !empty($where['apply_end'])){
            $start = strtotime($where['apply_start']);
            $end = strtotime($where['apply_end']);
            $_where[" apply_time_ext>=$start AND apply_time_ext<=$end AND 1=?"]=1;
        }
        $_where[" status IN('已批准','未审核') AND 1=?"]=1;

        //count
        $count = $this->get_leave_dao()->find_count($_where);
        if(!$count){
            return $data;
        }
        $leave_data = $this->get_leave_dao()->find($_where,$order,$limit,$offset);
        if(empty($leave_data)){
            return $data;
        }
        //如果只选了时间，这里再取用户信息
        if(empty($user_data)){
            $t_uids = array();
            foreach($leave_data as $k=>$val){
                $t_uids[]=$val['user_id'];
            }
            $t_uids = array_unique($t_uids);
            $_u_where[" user_id IN(".implode(',',$t_uids).") AND 1=?"]=1;
            $user_data = $this->get_user_info($_u_where);
        }
        //进行数据格式化输出
        foreach($leave_data as $k=>$val){
            $val['userinfo'] = $user_data[$val['user_id']];
            $val['apply_time'] =substr($val['apply_time'],0,-3);
            $leave_data[$k]=$val;
        }
        return array('count'=>$count,'datalist'=>$leave_data);
    }
    /**
     * 取符合条件的用户信息
     * @param unknown_type $where
     * @return multitype:|multitype:unknown
     */
    private function get_user_info($where){
        $data=array();
        $user_bll = new Bll_Attendance_UserInfo();
        $tmp_user_data = $user_bll->get_user_by_condition($where);
        if(empty($tmp_user_data)){
            return $data;
        }
        foreach($tmp_user_data as $val){
            $data[$val['user_id']]=$val;
        }
        return $data;
    }
    //leave_record
    private function get_leave_dao(){
        $dao = $this->get_dao('Dao_Attendance_LeaveRecord');
        return $dao;
    }
}